package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    // 定义四个tab的实例
    private Fragment tab1 = new Tab1();
    private Fragment tab2 = new Tab2();
    private Fragment tab3 = new Tab3();
    private Fragment tab4 = new Tab4();

    // 声明存储bottom中四个LinearLayout的对象
    private LinearLayout tabLayoutTalk;
    private LinearLayout tabLayoutFriends;
    private LinearLayout tabLayoutSocial;
    private LinearLayout tabLayoutSettle;

    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;

    private ImageView imageView;
    private ImageView imageView2;
    private ImageView imageView3;
    private ImageView imageView4;



    // 定义FragmentManager的对象
    private FragmentManager fm;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 取消标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        // 调用相关函数
        initFragment();
        initView();
        initEvent();
        // 应用开启后，默认选择第一个
        setSelect(0);

    }

    // 将四个tab实例，压入到FragmentManager栈中
    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.id_content, tab1);
        transaction.add(R.id.id_content, tab2);
        transaction.add(R.id.id_content, tab3);
        transaction.add(R.id.id_content, tab4);
        transaction.commit();

        hideFragment(transaction);
    }

    // 隐藏四个fragment
    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(tab1);
        transaction.hide(tab2);
        transaction.hide(tab3);
        transaction.hide(tab4);
    }

    // 获取需要用到的视图元素
    private void initView() {
        tabLayoutTalk = findViewById(R.id.tabTalk);
        tabLayoutFriends = findViewById(R.id.tabFriend);
        tabLayoutSocial = findViewById(R.id.tabSocial);
        tabLayoutSettle = findViewById(R.id.tabSettle);

        text1 = findViewById(R.id.textView1);
        text2 = findViewById(R.id.textView2);
        text3 = findViewById(R.id.textView3);
        text4 = findViewById(R.id.textView4);

        imageView = findViewById(R.id.imageView);
        imageView2 = findViewById(R.id.imageView2);
        imageView3 = findViewById(R.id.imageView3);
        imageView4 = findViewById(R.id.imageView4);
    }

    // 初始化监听事件
    private void initEvent() {
        tabLayoutTalk.setOnClickListener(this);
        tabLayoutFriends.setOnClickListener(this);
        tabLayoutSocial.setOnClickListener(this);
        tabLayoutSettle.setOnClickListener(this);
    }

    // 重写监听函数
    @Override
    public void onClick(View v) {

        // 用户点击任一按钮时，将所有按钮重置为原来的颜色
        resetImgText();

        switch (v.getId()){
            case R.id.tabTalk:
                setSelect(0);
                break;
            case R.id.tabFriend:
                setSelect(1);
                break;
            case R.id.tabSocial:
                setSelect(2);
                break;
            case R.id.tabSettle:
                setSelect(3);
                break;
            default:
                break;
        }
    }

    // 定义监听的处理函数
    private void setSelect(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        // 侦听到点击事件，则隐藏所有片段，以便显示用户点击的片段
        hideFragment(transaction);
        switch (i){
            case  0:
                transaction.show(tab1);
                text1.setTextColor(Color.parseColor("#07C160"));
                imageView.setImageResource(R.drawable.duihua2);
                break;
            case  1:
                transaction.show(tab2);
                text2.setTextColor(Color.parseColor("#07C160"));
                imageView2.setImageResource(R.drawable.pengyoufill2);
                break;
            case  2:
                transaction.show(tab3);
                text3.setTextColor(Color.parseColor("#07C160"));
                imageView3.setImageResource(R.drawable.tongxunlutianchong2);
                break;
            case  3:
                transaction.show(tab4);
                text4.setTextColor(Color.parseColor("#07C160"));
                imageView4.setImageResource(R.drawable.shezhi2);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    // 定义将所有图片与文字重置为原来颜色的函数
    public void resetImgText() {
        text1.setTextColor(Color.parseColor("#FFFFFF"));
        text2.setTextColor(Color.parseColor("#FFFFFF"));
        text3.setTextColor(Color.parseColor("#FFFFFF"));
        text4.setTextColor(Color.parseColor("#FFFFFF"));

        imageView.setImageResource(R.drawable.duihua);
        imageView2.setImageResource(R.drawable.pengyoufill);
        imageView3.setImageResource(R.drawable.tongxunlutianchong);
        imageView4.setImageResource(R.drawable.shezhi);
    }


}







